首页 > 试题广场 >

调整数组顺序使奇数位于偶数前面(二)

[编程题]调整数组顺序使奇数位于偶数前面(二)
  • 热度指数:21545 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入一个长度为 n 整数数组,数组里面可能含有相同的元素,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前面部分,所有的偶数位于数组的后面部分,对奇数和奇数,偶数和偶数之间的相对位置不做要求,但是时间复杂度和空间复杂度必须如下要求。

数据范围:,数组中每个数的值
要求:时间复杂度 ,空间复杂度
示例1

输入

[1,2,3,4]

输出

[1,3,2,4]

说明

[3,1,2,4]或者[3,1,4,2]也是正确答案 
示例2

输入

[1,3,5,6,7]

输出

[1,3,5,7,6]

说明

[3,1,5,7,6]等也是正确答案 
示例3

输入

[1,4,4,3]

输出

[1,3,4,4]
class Solution:
    def reOrderArrayTwo(self, array: List[int]) -> List[int]:
        # write code here
        myarray = []

        for i in array:
            if i % 2 == 1:
                myarray.append(i)
        for i in array:
            if i % 2 == 0:
                myarray.append(i)
        return myarray
发表于 2023-10-20 20:34:28 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        return sorted(array, key=lambda x:x%2, reverse=True)

发表于 2022-07-03 08:38:19 回复(0)
双指针
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        left=0
        right=len(array)-1
        while left<right:
            while left<right and array[left]%2==1:
                left+=1
            while left<right and array[right]%2==0:
                right-=1
            array[left], array[right] = array[right], array[left]
            left+=1
            right-=1
        return array


发表于 2022-06-17 15:49:17 回复(0)
这个和前面那个进阶进阶在哪里了?
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        l1 = []
        l2 = []
        for i in array:
            if i %2 == 0:
                l2.append(i)
            else:
                l1.append(i)
        return l1+l2


发表于 2022-06-15 17:35:08 回复(0)
class Solution:
    # 双指针对撞(不能保证相对位置不变)
    # 时间复杂度:O(n)  空间复杂度:O(1)
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        i = 0
        j = len(array) - 1
        while i < j:
            if (array[i] % 2) and not (array[j] % 2):  # 前奇后偶
                i += 1
                j -= 1
            elif (array[i] % 2) and (array[j] % 2):  # 前奇后奇
                i += 1
            elif not (array[i] % 2) and not (array[j] % 2):  # 前偶后偶
                j -= 1
            else:  # 前偶后奇
                array[i], array[j] = array[j], array[i]
                i += 1
                j -= 1
        return array

class Solution:
    # 双指针 + 新的数组
    # 时间复杂度:O(n)  空间复杂度:O(1)
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        n = len(array)
        res = [0 for i in range(n)]
        odd = 0
        for i in array:
            if i % 2:
                odd += 1
        x = 0
        y = odd 
        for a in array:
            if a % 2:
                res[x] = a 
                x += 1
            else:
                res[y] = a 
                y += 1
        return res
发表于 2022-05-12 19:44:05 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        odd = []
        even = []
        for n in array:
            if n%2:
                odd.append(n)
            else:
                even.append(n)
        return odd+even

发表于 2022-04-22 14:38:16 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        array.sort(key = lambda x:x%2==1, reverse=True)
        return array


发表于 2022-04-15 13:20:50 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        nums = array
        left = 0
        right = len(nums) - 1
        while left < right:
            if nums[left] % 2 == 0:
                nums[left], nums[right] = nums[right], nums[left]
                right -= 1
            else:
                left += 1
        return nums

发表于 2022-03-21 23:54:34 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        i_even, i_odd = 0, len(array) - 1
        while i_even < i_odd:
            if array[i_even] % 2 == 0:
                while i_odd > i_even:
                    if array[i_odd] % 2 == 1:
                        array[i_even], array[i_odd] = array[i_odd], array[i_even]
                        break
                    i_odd -= 1

            i_even += 1

        return array
发表于 2022-03-10 16:22:21 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#

# @param array int整型一维数组 
# @return int整型一维数组
#
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        l, r = 0, len(array) - 1
        while l < r:
            if array[l] % 2:
                l += 1
                continue
            if array[r] % 2 == 0:
                r -= 1
                continue
            array[l], array[r] = array[r], array[l]
            l += 1
            r -= 1
        return array
发表于 2022-03-01 19:14:44 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        # 双指针, 前指针只找偶数, 后指针只找奇数, 找到后互换。
        i = 0
        j = len(array) - 1
        while i < j:
            a = array[i]
            b = array[j]
            if b % 2 == 1 and a % 2 == 0:
                array[i], array[j] = array[j], array[i]
                i+=1
                j-=1
                continue
            if b % 2 == 0:
                j-=1
            if a %  2 == 1:
                i+=1
        return array
发表于 2022-02-24 23:57:13 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        i=0
        j=len(array)-1
        while i<j:
            if array[i]%2==0 and array[j]%2!=0:
                array[i],array[j]=array[j],array[i]
                i+=1
                j-=1
            elif array[i]%2!=0 and array[j]%2==0:
                i+=1
                j-=1
            elif array[i]%2==0 and array[j]%2==0:
                j-=1
            elif array[i]%2!=0 and array[j]%2!=0:
                i+=1
        return array

发表于 2022-01-26 23:29:09 回复(0)
class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        # write code here
        # 快排思想
        i, j = 0, len(array)-1
        while i < j:
            while i<j and array[i] %2 ==1:
                i+=1
            while i<j and array[j]%2 == 0:
                j-=1
            array[i], array[j] = array[j], array[i]
        return array

发表于 2021-12-10 15:22:00 回复(0)

class Solution:
    def reOrderArrayTwo(self , array: List[int]) -> List[int]:
        a = []
        local = 0
        for i in array:
            if i % 2 == 1:
                a.insert(local, i)
                local += 1
            else:
                a.append(i)
        return a

发表于 2021-11-29 09:17:51 回复(0)

问题信息

上传者:牛客301499号
难度:
24条回答 4100浏览

热门推荐

通过挑战的用户

查看代码